package com.sky.service.impl;


import ch.qos.logback.core.status.Status;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;

import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;

import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * 分类业务层
 */
@Service
@Slf4j
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    /**
     * 新增菜品
     *
     * @param dishDTO
     */
    @Override
    @Transactional
    public void addDish(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        //1.填充数据层实体类
        //2.新增菜品信息
        dishMapper.addDish(dish);
        //3.封装菜品口味数据信息
        List<DishFlavor> flavors = dishDTO.getFlavors();
        flavors.stream().forEach(f->f.setDishId(dish.getId()));
        //4.新增菜品口味数据
        dishFlavorMapper.addDishFlavors(flavors);
    }

    /**
     * 分页查询菜品
     *
     * @param dishDTO
     * @return
     */
    @Override
    public PageResult pageQuery(DishPageQueryDTO dishDTO) {
        //包装分页参数
        PageHelper.startPage(dishDTO.getPage(),dishDTO.getPageSize());

        //使用关联sql查询出菜品和分类名称
        Page<DishVO> page = dishMapper.pageQuery(dishDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 根据id批量删除菜品
     *
     * @param ids
     */
    @Override
    @Transactional
    public void deleteDishs(List<Long> ids) {
        //1、菜品有无在售状态
        int num = dishMapper.isStatusUsing(ids);
        if(num > 0){
            //当前菜品处于起售中，不能删除
            throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
        }
        //2、先判断菜品有没有被套餐关联
        num = dishMapper.isMealJoin(ids);
        if(num > 0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        //3、删除菜品关联所有口味
        //4、删除所有菜品
        for (Long id : ids) {
            dishMapper.DishFlavorMapper(id);
            dishMapper.deleteDishByIds(id);
        }

    }

    /**
     * 根据id查询菜品
     *
     * @param id
     * @return
     */
    @Override
    public DishVO getByID(Long id) {
        return dishMapper.getById(id);
    }

    /**
     * 根据id修改菜品
     *
     * @param dishDTO
     */
    @Override
    @Transactional
    public void updateDish(DishDTO dishDTO) {
        Long id = dishDTO.getId();
        if(id == null){
            throw new DeletionNotAllowedException("id不能为空");
        }
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(!ObjectUtils.isEmpty(flavors)){
            //1.将菜品口味删除
            dishMapper.deleteDishByIds(id);
            //2.新增菜品口味信息
            flavors.stream().forEach(f->f.setDishId(id));
            dishFlavorMapper.addDishFlavors(flavors);
        }
        //3.修改菜品信息
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.update(dish);
    }

    /**
     * 停售商品
     *
     * @param id
     * @param status
     */
    @Override
    public void updateStatuDish(Long id, Integer status) {
        Dish dish = Dish.builder().id(id).status(status).build();
        dishMapper.update(dish);
    }

    /**
     * 查询菜品列表
     *
     * @param categoryId
     * @return
     */
    @Override
    public List<DishVO> dishList(Long categoryId,String name) {
        return dishMapper.selectDishList(categoryId,name);
    }

    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<DishVO> dishList = dishMapper.list(dish);

        return dishList;
    }
}
